package com.luluteam.pushlib;

import android.app.Application;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;

import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.constant.CtxConstant;
import com.luluteam.pushlib.service.mqtt.MqttService;
import com.luluteam.pushlib.tools.JumpHelper;
import com.luluteam.pushlib.tools.MsgLifecycleCallback;
import com.luluteam.pushlib.util.PermissionUtil;
import com.luluteam.pushlib.util.SystemUtils;
import com.luluteam.pushlib.util.ThreadUtil;


/**
 * @author Guan
 * @date Created on 2018/4/2
 */
public class ContextClient {

    private static ContextClient instance;
    private static final String TAG = "ContextClient";

    //===================================

    /**
     * SDK 的入口
     *
     * @param context
     */
    public static ContextClient init(Application context, String APP_ID) {
        if (instance == null) {
            synchronized (ContextClient.class) {
                if (instance == null) {
                    instance = new ContextClient(context, APP_ID);
                    //开启APP的时候上报一次情景消息
                    postContext();
                }
            }
        }

        return instance;
    }


    /**
     * 手动上报情景信息
     */
    public static void postContext() {
        if (instance == null) {
            Log.e(TAG, "操作失败：Context Client 未初始化");
            return;
        }
        JumpHelper.gotoLocationService(CtxConstant.ACTION_NORMAL, "");
    }

    /**
     * 请求服务器进行基于情景上下文的推荐
     */
    public static void requestYJingPush(String jsonTag) {
        if (instance == null) {
            Log.e(TAG, "操作失败：Context Client 未初始化");
            return;
        }
        JumpHelper.gotoLocationService(CtxConstant.ACTION_CONTEXT, jsonTag);
    }


    /**
     * 上报情景数据
     *
     * @param messageId Message的唯一标识
     * @param rating    评分
     * @param duration  阅读时间
     * @param wordCount 字数
     */
    public static void rating(String messageId, int rating, long duration, int wordCount) {
        if (instance == null) {
            Log.e(TAG, "操作失败：Context Client 未初始化");
            return;
        }
        JumpHelper.gotoLocationService(messageId, rating, duration, wordCount);
    }

    //================================

    private ContextClient(Application context, String APP_ID) {
        AppHolder.appContext = context;
        //获取AndroidId，用于标识设备
        AppHolder.androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        //deviceId
        AppHolder.deviceId = SystemUtils.getInstance(context).getUniqueDeviceID();
        //
        AppHolder.APP_ID = APP_ID;

        //启动
        if (ThreadUtil.checkRunOnMainThread()
                && PermissionUtil.checkPermission(AppHolder.appContext, AppHolder.PERMISSIONS)) {
            Log.i(TAG, "Context Client 启动成功");
        } else {
            Log.e(TAG, "Context Client 启动失败");
        }
        start();
    }


    //============================


    /**
     * 做一些其他信息的启动
     */
    private void start() {

        //启动监听activity的生命周期
        ((Application) AppHolder.appContext).registerActivityLifecycleCallbacks(new MsgLifecycleCallback());
        //开启Mqtt，启动消息推送的监听
//        AppHolder.appContext.startService(new Intent(AppHolder.appContext, MQTTService.class));
        AppHolder.appContext.startService(new Intent(AppHolder.appContext, MqttService.class));

    }


}
